/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package updatetree;

import java.util.ArrayList;

/**
 *
 * @author mani
 */
public class index {

    private double[][] data;

    public index(ArrayList<double[]> dataParam) {
        data = new double[dataParam.size()][dataParam.get(0).length];
        for (int i = 0; i < dataParam.size(); i++) {
            double[] ds = dataParam.get(i);
            for (int j = 0; j < ds.length; j++) {
                double d = ds[j];
                data[i][j] = d;
            }
        }
    }

   ArrayList<double[]>  getComputedData() {
        ArrayList<double[]> comuputeddata = new ArrayList<double[]>();
        for (int i = 20; i < data.length-1; i++) {
            ArrayList<Double> indexarray = new ArrayList<Double>();
            //  indexarray.add(WR4(data, i));
            indexarray.add(kairi(data, i));
            indexarray.add(osc10(data, i));
            indexarray.add(BIAS(data, i));
            indexarray.add(RSI4(data, i));
            indexarray.add(RSV4(data, i));
            indexarray.add(ROC3(data, i));
            indexarray.add(VROC4(data, i));
            indexarray.add(today(data, i));
           indexarray.add(tormorrow(data, i));
            int fieldcount = indexarray.size();
            double[] tmpAL = new double[indexarray.size()];
            for (int j = 0; j < indexarray.size(); j++) {
                Double double1 = indexarray.get(j);
                tmpAL[j] = double1.doubleValue();
            }
            comuputeddata.add(tmpAL);
        }
        return comuputeddata;
    }

    private double kairi(double[][] stockdata, int k) {
        return (stockdata[k][3] - m4(stockdata, k)) / m4(stockdata, k) * 100;

    }

    private double m4(double[][] stockdata, int i) {
        double sum = 0;
        for (int j = i - 3; j <= i; j++) {
            sum = sum + stockdata[j][3];
        }
        return sum / 4.0;
    }

    private double osc10(double[][] stockdata, int i) {
        return stockdata[i][3] - m10(stockdata, i);
    }

    private double m10(double[][] stockdata, int i) {
        double sum = 0;
        for (int j = i - 9; j <= i; j++) {
            sum = sum + stockdata[j][3];
        }
        return sum / 10.0;
    }

    private double BIAS(double[][] stockdata, int i) {
        return ((stockdata[i][3] - m4(stockdata, i)) / m4(stockdata, i)) * 100;
    }

    private double RSI4(double[][] stockdata, int i) {
        double x = stockdata[i - 3][3];
        double sump = 0;
        double sumdown = 0;
        int j = i - 3;
        for (; j <= i; j++) {
            double v = stockdata[j][3];
            if (x - v > 0) {
                sumdown += (x - v);
            }
            if (x - v < 0) {
                sump += (v - x);
            }
            x = stockdata[j][3];
        }

        return ((sump / 4.0) / ((sump / 4.0) + sumdown / 4.0)) * 100;
    }

    private double RSV4(double[][] stockdata, int i) {
        int j = i - 3;
        double max = stockdata[j][3];
        double min = max;
        for (j++; j <= i; j++) {
            double m = stockdata[j][3];
            if (m < min) {
                min = m;
            }
            if (m > max) {
                max = m;
            }
        }


        return (stockdata[i][3] - min) / (max - min) * 100;

    }

    private double ROC3(double[][] stockdata, int i) {
        double ax = stockdata[i][3] - stockdata[i - 3][3];
        double bx = stockdata[i - 3][3];

        return ax / bx * 100;
    }

    private double VROC4(double[][] stockdata, int i) {
        return ((stockdata[i][4] - stockdata[i - 3][4]) / stockdata[i - 3][4]) * 100;//V1
    }

    private double today(double[][] stockdata, int i) {
        return (stockdata[i][3] - stockdata[i - 1][3]) / stockdata[i - 1][3];
    }

    private double tormorrow(double[][] stockdata, int i) {
        return (stockdata[i + 1][3] - stockdata[i][3]) / stockdata[i][3];
    }
}
